# Borland make does not understand the following directive
ifndef MAKE

# So it halts here
!error 1 Do not run with borland make!

endif

# WARNING: Do not use include directive!
# We need to call ourselves, and MAKEFILE_LIST has problem handling names with spaces
# So the safest way is to ensure it has only one file name -- thus no "include"
# NOTE: One way to solve the problem is to remove the need of recursive calls
#       This can be done by using eval and generating targets on the fly
SELF="$(strip $(MAKEFILE_LIST))"

# Need BDS environment
ifndef BDS

# Otherwise halt here
all %:
	$(error Need BDS environment)

else

# Need project build target
ifndef PROJECTTARGET

# Otherwise halt here
all %:
	$(error Need project build target)

else

PROJECTTARGET:=$(strip $(PROJECTTARGET))

# Need GNUMake toolset path
ifndef GNUMake

# Otherwise halt here
all %:
	$(error Need GNUMake toolset path)

else

.PHONY: all

# COMMON UTILITIES (NO NEED TO CHANGE NORMALLY)
MAKE="$(GNUMake)\make"
DCC="$(BDS)\bin\dcc32"

# BUILD TARGETS (NO NEED TO CHANGE NORMALLY)
TARGETS=Debug Release
.PHONY: $(TARGETS)

Debug: BUILDDIR=..\..\Bin\Debug\Units
Debug: BUILDFLAGS=-$$$$D+ -$$$$C+ -GD

Release: BUILDDIR=..\..\Bin\Release\Units
Release: BUILDFLAGS=-$$$$D- -$$$$C- -GP

# SPECIAL DIRECTIVES (NO NEED TO CHANGE NORMALLY)
SLASH=\

.EXPORT_ALL_VARIABLES:

# BUILD UNITS
UNITS=System.dcu SysInit.dcu

ifeq ($(MAKELEVEL), 0)

# TOP LEVEL BUILD TARGETS (NO NEED TO CHANGE NORMALLY)
REMAKE=$(MAKE) -f$(SELF) --no-print-directory

all: $(TARGETS)

$(TARGETS):
	$(REMAKE) $(foreach UNIT,$(UNITS),$(BUILDDIR)\$(UNIT))

.DEFAULT:
	$(error Valid build targets are: $(TARGETS) all)

else

ifeq "$(PROJECTTARGET)" "<CLEAN>"

# CLEAN RULES (NO NEED TO CHANGE NORMALLY)

.PHONY: $(foreach UNIT,$(UNITS),$(BUILDDIR)\$(UNIT))

$(foreach UNIT,$(UNITS),$(BUILDDIR)\$(UNIT)):
	if exist "$@" del "$@"

else

######################################
# BUILD RULE FOR EACH UNIT GOES HERE #
######################################

SOURCE_System= BaseRTL.inc \
System.pas \
System.RTTI.pas \
System.Types.pas \
System.Set.pas \
System.Int64.pas \
System.Utils.pas \
System.Logging.pas \
System.Processor.pas \
System.Error.pas \
System.Exception.pas \
System.Modular.pas \
System.Modular.EXE.pas \
System.Modular.DLL.pas \
System.Threading.pas \
System.Coverage.pas \
System.Extension.pas \
System.Resource.pas \
System.Resource.InitImport.pas \
System.MemoryManager.pas \
System.ShortString.pas \
System.ANSIString.pas \
System.WideString.pas \
System.UnicodeString.pas \
System.StringConv.pas \
System.Variant.pas \
System.Array.pas \
System.Record.pas \
System.GUID.pas \
System.File.pas \
System.TextFile.pas \
System.Console.pas \
System.ObjectInterface.pas \
System.WinAPI.pas \
SysInit.pas \
SysInit.TLS.pas \
SysInit.Modular.pas \
SysInit.Modular.EXE.pas \
SysInit.Modular.DLL.pas \
SysInit.Modular.BPL.pas \
SysInit.Extension.pas \

%$(SLASH)System.dcu: $(SOURCE_System)
	$(DCC) --no-config "System.pas" -D$(DEFINES) -M -Y -Q $(BUILDFLAGS) -U"$(BUILDDIR)" -N0"$(BUILDDIR)"
	if exist "$(PROJECTTARGET)" del "$(PROJECTTARGET)"

%$(SLASH)SysInit.dcu: $(SOURCE_System)
	$(DCC) --no-config "SysInit.pas" -D$(DEFINES) -M -Y -Q $(BUILDFLAGS) -U"$(BUILDDIR)" -N0"$(BUILDDIR)"
	if exist "$(PROJECTTARGET)" del "$(PROJECTTARGET)"

endif # $(PROJECTTARGET) = "<CLEAN>"
endif # $(MAKELEVEL) = 0
endif # GNUMake is set
endif # PROJECTTARGET is set
endif # BDS is set